#include "h_adc.h"
#include "fsl_debug_console.h"
#include <stdio.h>

volatile bool g_VAdcConversionDoneFlag;
volatile bool g_CAdcConversionDoneFlag;

volatile uint32_t g_VAdcConversionValue;
volatile uint32_t g_CAdcConversionValue;

volatile uint32_t g_VAdcInterruptCounter;
volatile uint32_t g_CAdcInterruptCounter;


const uint32_t g_Adc_12bitFullRange = 4096U;

static adc_channel_config_t VadcChannelConfigStruct;
static adc_channel_config_t CadcChannelConfigStruct;



void voltage_ADC_IRQHandler(void)
{
	g_VAdcConversionDoneFlag = true;
    /* Read conversion result to clear the conversion completed flag. */
	g_VAdcConversionValue = ADC_GetChannelConversionValue(voltage_ADC_BASE, voltage_ADC_CHANNEL_GROUP);
	g_VAdcInterruptCounter++;
    SDK_ISR_EXIT_BARRIER;
}


void current_ADC_IRQHandler(void)
{
	g_CAdcConversionDoneFlag = true;
    /* Read conversion result to clear the conversion completed flag. */
	g_CAdcConversionValue = ADC_GetChannelConversionValue(current_ADC_BASE, current_ADC_CHANNEL_GROUP);
	g_CAdcInterruptCounter++;
    SDK_ISR_EXIT_BARRIER;
}


void adc_init()
{
	adc_config_t adcConfigStruct;


    EnableIRQ(voltage_ADC_IRQn);
    EnableIRQ(current_ADC_IRQn);

    /*
     *  config->enableAsynchronousClockOutput = true;
     *  config->enableOverWrite =               false;
     *  config->enableContinuousConversion =    false;
     *  config->enableHighSpeed =               false;
     *  config->enableLowPower =                false;
     *  config->enableLongSample =              false;
     *  config->referenceVoltageSource =        kADC_ReferenceVoltageSourceVref;
     *  config->samplePeriodMode =              kADC_SamplePeriod2or12Clocks;
     *  config->clockSource =                   kADC_ClockSourceAD;
     *  config->clockDriver =                   kADC_ClockDriver1;
     *  config->resolution =                    kADC_Resolution12Bit;
     */

    ADC_GetDefaultConfig(&adcConfigStruct);

    ADC_Init(voltage_ADC_BASE, &adcConfigStruct);
    ADC_Init(current_ADC_BASE, &adcConfigStruct);


    /* Do auto hardware calibration. */
    if (kStatus_Success == ADC_DoAutoCalibration(voltage_ADC_BASE) && kStatus_Success == ADC_DoAutoCalibration(current_ADC_BASE))
    {
        PRINTF("ADC_DoAutoCalibration() Done.\r\n");
    }
    else
    {
        PRINTF("ADC_DoAutoCalibration() Failed.\r\n");
    }

    VadcChannelConfigStruct.channelNumber                        = voltage_ADC_USER_CHANNEL;
    VadcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
    g_VAdcInterruptCounter                                       = 0U; /* Clear the interrupt counter. */

    CadcChannelConfigStruct.channelNumber                        = current_ADC_USER_CHANNEL;
    CadcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
    g_CAdcInterruptCounter                                       = 0U; /* Clear the interrupt counter. */

}

float read_Vadc()
{
	float value = 0.00;
	g_VAdcConversionDoneFlag = false;
	ADC_SetChannelConfig(voltage_ADC_BASE, voltage_ADC_CHANNEL_GROUP, &VadcChannelConfigStruct);
	while (g_VAdcConversionDoneFlag == false)
	{
	}
	value = (float)(g_VAdcConversionValue + 1) / (float)g_Adc_12bitFullRange;
//	value = ((float)(g_VAdcConversionValue/g_Adc_12bitFullRange)) * 3.3;
//	PRINTF(" g_VAdcConversionValue:%d\r\n",g_VAdcConversionValue);

	return value;
	}


float read_Cadc()
{
	float value = 0.00;
	g_CAdcConversionDoneFlag = false;
	ADC_SetChannelConfig(current_ADC_BASE, current_ADC_CHANNEL_GROUP, &CadcChannelConfigStruct);
	while (g_CAdcConversionDoneFlag == false)
	{
	}
	value = (float)(g_CAdcConversionValue + 1) / (float)g_Adc_12bitFullRange;
//	value = ((float)(g_VAdcConversionValue/g_Adc_12bitFullRange)) * 3.3;
//	PRINTF(" g_CAdcConversionValue:%d\r\n",g_CAdcConversionValue);

	return value;
	}
